home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 4 / FM Towns Free Software Collection 4 - Disc 1.iso / t_os / look / source / loader.c < prev    next >
Text File  |  1991-10-18  |  6KB  |  325 lines

  1. /*                                                */
  2. /*    Gear Box Music Loader v0.2                    */
  3. /*                                                */
  4. /*        1988/11/01                                */
  5. /*        Copyright (C) FUJITSU LIMITED             */
  6. /*        Programed by Joe Mizuno (FPS)             */
  7. /*        Small Cheng By Ken                        */
  8. /*                                                */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <snd.h>
  13.  
  14. static int buff_size;
  15. static int buff_sw;
  16. static char *buff_top[2];
  17. static char *buff_btm[2];
  18. static char status_save;
  19.  
  20. static char *bios_work;
  21. static int tempo,signature;
  22. static int eof;
  23. static FILE *fp;
  24.  
  25. /*------------------------------------------------*/
  26. int ML_Start(buffer,size,work)
  27. char *buffer;
  28. int size;
  29. char *work;
  30. {
  31.     bios_work = work;
  32.     if((buff_size = (size/12)*6) < 64)
  33.         return(2);
  34.  
  35.     buff_top[0] = buffer;
  36.     buff_btm[0] = buff_top[0] + buff_size -6;
  37.     buff_top[1] = buff_top[0] + buff_size;
  38.     buff_btm[1] = buff_top[1] + buff_size -6;
  39.  
  40.     SND_eup_init(bios_work);
  41.     return(0);
  42. }
  43. /*------------------------------------------------*/
  44. int ML_End()
  45. {
  46.     SND_eup_end();
  47.     return(0);
  48. }
  49.  
  50. /*------------------------------------------------*/
  51. char    *getins(env,file)
  52. char    *env,*file;
  53. {
  54.     static char tmp[128];
  55.     char *p;
  56.  
  57.     if ( (p = getenv(env)) == NULL )
  58.     return NULL;
  59.     sprintf(tmp,"%s\\%s",p,file);
  60.     return tmp;
  61. }
  62. int ML_Set(path)
  63. char *path;
  64. {
  65.     int i,j,err,size;
  66.     char buffer[128];
  67.     char bankpath[128];
  68.     char fm_bankname[16];
  69.     char pcm_bankname[16];
  70.     char *p;
  71.  
  72.     eof = 1;
  73.     if((fp = fopen(path,"rb")) == NULL)
  74.         return(1);
  75.  
  76.     fseek(fp,852,SEEK_SET);
  77.  
  78.     if(fread(buffer,1,32,fp) < 32)
  79.         goto ERROR;
  80.     for(i=0 ; i<32 ;i++)
  81.         err=SND_eup_mute_set(i,(int)buffer[i]);
  82.  
  83.     if(fread(buffer,1,32,fp) < 32)
  84.         goto ERROR;
  85.     for(i=0 ; i<32 ;i++)
  86.         err=SND_eup_port_set(i,(int)buffer[i]);
  87.  
  88.     if(fread(buffer,1,32,fp) < 32)
  89.         goto ERROR;
  90.     for(i=0 ; i<32 ;i++)
  91.         err=SND_eup_midi_ch_set(i,(int)buffer[i]);
  92.  
  93.     if(fread(buffer,1,32,fp) < 32)
  94.         goto ERROR;
  95.     for(i=0 ; i<32 ;i++)
  96.         err=SND_eup_bias_set(i,(int)buffer[i]);
  97.  
  98.     if(fread(buffer,1,32,fp) < 32)
  99.         goto ERROR;
  100.     for(i=0 ; i<32 ;i++)
  101.         err=SND_eup_transpose_set(i,(int)buffer[i]);
  102. /*
  103. channel assign
  104. */
  105.     fseek(fp,1748,SEEK_SET);
  106.     if(fread(buffer,1,6,fp) < 6)
  107.         goto ERROR;
  108.     for(i = 0;i < 6;i++)
  109.         err=SND_midi_ch_assign(i,(int)buffer[i]);
  110.  
  111.     if(fread(buffer,1,8,fp) < 8)
  112.         goto ERROR;
  113.     for(i = 0;i < 8;i++)
  114.         err=SND_midi_ch_assign(i+64,(int)buffer[i]);
  115. /*
  116. bank load
  117. */
  118.     SND_pcm_mode_set(0);
  119.     
  120.     for(i = 0;path[i] != '\0';i++)
  121.         bankpath[i] = path[i];
  122.     while((path[i] != '\\') && (i >= 0))i--;
  123.     i++;
  124.  
  125.     bankpath[i+8] = '\0';
  126.     if(fread(&bankpath[i],1,8,fp) < 8)
  127.         goto ERROR;
  128.     if(bankpath[i] != '\0'){
  129.         strcat(&bankpath[i],".FMB");
  130.         p = bankpath;
  131.         if ( SND_fm_bank_load(p,fm_bankname) != 0 ) {
  132.             if ( (p = getins("FMINST",&bankpath[i])) == (char *)0 ||
  133.                  SND_fm_bank_load(p,fm_bankname) != 0 )
  134.             DSP_error("Can't open/read '%s'",p);
  135.         }
  136.     }
  137.  
  138.     bankpath[i+8] = '\0';
  139.     if(fread(&bankpath[i],1,8,fp) < 8)
  140.         goto ERROR;
  141.     if(bankpath[i] != '\0'){
  142.         strcat(&bankpath[i],".PMB");
  143.         p = bankpath;
  144.         if ( SND_pcm_bank_load(p,pcm_bankname) != 0 ) {
  145.              if ( (p = getins("PCMINST",&bankpath[i])) == (char *)0 ||
  146.                   SND_pcm_bank_load(p,pcm_bankname) != 0 )
  147.             DSP_error("Can't open/read '%s'",p);
  148.         }
  149.     }
  150.  
  151.     fseek(fp,2048,SEEK_SET);
  152.  
  153.     if(fread(&size,4,1,fp) < 1)
  154.         goto ERROR;
  155.     if(fread(buffer,1,2,fp) < 2)
  156.         goto ERROR;
  157.     signature = (int)buffer[0];
  158.     tempo = (int)buffer[1];
  159.  
  160.     eof = 0;
  161.     status_save = 0xfe;
  162.     buff_sw = 1;
  163.     load_sub();
  164.     load_sub();
  165.     SND_eup_tempo_set(tempo);
  166.     return(0);
  167. ERROR:
  168.     fclose(fp);
  169.     return(2);
  170. }
  171.  
  172. /*------------------------------------------------*/
  173. int ML_Play(mode)
  174. int mode;
  175. {
  176.     SND_eup_loop_set(mode);
  177.     SND_eup_play_start(buff_top[0],buff_size*2,signature);
  178.     return(0);
  179. }
  180.  
  181. /*------------------------------------------------*/
  182. int ML_Stop()
  183. {
  184.     SND_eup_play_stop();
  185.     if(eof == 0)
  186.         fclose(fp);
  187.     return(0);
  188. }
  189.  
  190. /*------------------------------------------------*/
  191. int ML_Check()
  192. {
  193.     char *ptr;
  194.  
  195.         if(eof != 0)
  196.             return(0);
  197.         ptr = SND_eup_stat_ptr();
  198.  
  199.         if(((buff_sw == 0) && (ptr <= buff_btm[0]))
  200.                 ||((buff_sw == 1) && (ptr >= buff_top[1])))
  201.             return(load_sub());
  202.         else
  203.             return(0);
  204.             
  205. }    
  206. /*------------------------------------------------*/
  207. int SL_Start()
  208. {
  209.     SND_pcm_sound_delete(-1);
  210.     SND_pcm_mode_set(1);
  211.     return(0);
  212. }
  213.  
  214. /*------------------------------------------------*/
  215. int SL_Load(path,buffer,size)
  216. char *path;
  217. char buffer[];
  218. int size;
  219. {
  220.  
  221.     FILE *fp;
  222.     int length;
  223.  
  224.     if((fp = fopen(path,"rb")) == NULL)
  225.         return(1);
  226.     if(size < 32)
  227.         goto ERROR;
  228.     if(fread(buffer,1,32,fp) < 32)
  229.         goto ERROR;
  230.     length = *(int *)&buffer[12];
  231.     if(size < length+32)
  232.         goto ERROR;
  233.     if(fread(buffer+32,1,length,fp) < length)
  234.         goto ERROR;
  235.     fclose(fp);
  236.     return(0);
  237. ERROR:
  238.     fclose(fp);
  239.     return(2);
  240. }
  241.  
  242. /*------------------------------------------------*/
  243. int SL_Play(mode,rate,vol,pan,buff)
  244. int mode;
  245. int rate;
  246. int vol;
  247. int pan;
  248. char buff[];
  249. {
  250.     int org_note;
  251.     int loop;
  252.  
  253.     if(mode > 127)
  254.         return(3);
  255.     SND_pan_set(71,pan);
  256.     if(rate){
  257.         rate = rate * 0x62 / 1000;
  258.         *(short int*)&buff[24] = rate;
  259.     }
  260.     if(mode==1)
  261.         *(int *)&buff[20] = 0;
  262.     else if(mode == 2){
  263.         loop = *(int *)&buff[12];
  264.         *(int *)&buff[16] = 0;
  265.         *(int *)&buff[20] = loop;
  266.     }
  267.  
  268.     org_note = buff[28];
  269.     return(SND_pcm_play(71,org_note,vol,buff));
  270.     
  271. }
  272.  
  273. /*------------------------------------------------*/
  274. int SL_Status()
  275. {
  276.     return(SND_pcm_status(71));
  277. }
  278.  
  279. /*------------------------------------------------*/
  280. int SL_Stop()
  281. {
  282.  
  283.     SND_pcm_play_stop(71);
  284.     SND_pcm_rec_stop();
  285.     return(0);
  286. }
  287.  
  288. /*------------------------------------------------*/
  289. int load_sub()
  290. {
  291.     char *ptr;
  292.     char status;
  293.     int sw;
  294.  
  295.     if(!buff_sw){
  296.         buff_sw = 1;
  297.         sw = 0;
  298.     }
  299.     else{
  300.         buff_sw = 0;
  301.         sw = 1;
  302.     }
  303.     ptr = buff_top[buff_sw];
  304.     while((!eof) && (ptr <= buff_btm[buff_sw])){
  305.         if(fread(ptr,1,6,fp) < 6){
  306.             fclose(fp);
  307.             return(2);
  308.         }
  309.         else if(*ptr == 0xfe){
  310.             eof = 1;
  311.             fclose(fp);
  312.         }
  313.         ptr = ptr + 6;
  314.     }
  315.  
  316.     status = status_save;
  317.     if(!eof){
  318.         status_save = *buff_btm[buff_sw];
  319.         *buff_btm[buff_sw] = 0xfd;
  320.     }
  321.     *buff_btm[sw] =  status;
  322.     SND_eup_play_restart();
  323.     return(0);
  324. }
  325.